<?php
// *** Tangra (Application Framework and Tools for PHP)
// $Id$
//

require_once(TANGRA_MAIN_DIR.'exceptions/te_web_site_config_error.class.php');
require_once(TANGRA_MAIN_DIR.'filesystem_toolbox/filesystem_functions.inc.php');


class Directory_Structure_Base_Config extends Tangra_Class {
	private $root;
	private $document_root;
	private $document_root_relative;
	private $hidden;
	private $scratch;
	private $ext_inc;

	public function set_root($root_path) {
		$root_path = trim($root_path);
		if (tangra_dir_exists($root_path)) {
			$this->root = tangra_normalize_path($root_path);
		} else {
			throw new TE_Web_Site_Config_Error('Invalid $root_path (directory_structure_base.conf - ROOT). Not exists or is not a dir. Current value: '.$root_path);
		}
	}


	public function get_root() {
		return $this->root;
	}


	public function set_document_root($docroot_path = NULL) {
		if ($docroot_path != NULL) {
			$this->_set_document_root($docroot_path);
		} else {
			$this->_set_document_root($this->get_root().'htdocs/');
		}
	}


	private function _set_document_root($docroot_path) {
		if (tangra_dir_exists($docroot_path)) {
			$this->document_root = tangra_normalize_path($docroot_path);
		} else {
			throw new TE_Web_Site_Config_Error('Invalid $docroot_path (DOCUMENT_ROOT). Not exists or is not a dir. Current value: '.$docroot_path);
		}
	}


	public function get_document_root() {
		return $this->document_root;
	}


	public function set_ducument_root_relative($path = NULL) {
		if ($path != NULL) {
			$this->document_root_relative = $path;
		} else {
			$this->document_root_relative = '/';
		}
	}


	public function get_document_root_relative() {
		return $this->document_root_relative;
	}


	public function set_hidden($hidden_path = NULL) {
		if ($hidden_path != NULL ) {
			$this->_set_hidden($hidden_path);
		} else {
			$this->_set_hidden($this->get_root().'hidden/');
		}
	}


	private function _set_hidden($hidden_path) {
		if (tangra_dir_exists($hidden_path)) {
			$this->hidden = tangra_normalize_path($hidden_path);
		} else {
			throw new TE_Web_Site_Config_Error('Invalid $hidden_path (HIDDEN). Not exists or is not a dir. Current value: '.$hidden_path);
		}
	}


	public function get_hidden() {
		return $this->hidden;
	}


	public function set_scratch($scratch_path = NULL) {
		if ($scratch_path != NULL) {
			$this->_set_scratch($scratch_path);
		} else {
			$this->_set_scratch($this->get_root().'scratch/');
		}
	}


	private function _set_scratch($scratch_path) {
		if (tangra_dir_exists($scratch_path)) {
			$this->scratch = tangra_normalize_path($scratch_path);
		} else {
			throw new TE_Web_Site_Config_Error('Invalid $scratch_path (MS_SCRATCH_PATH). Not exists or is not a dir. Current value: '.$scratch_path);
		}
	}


	public function get_scratch() {
		return $this->scratch;
	}


	/**
	 * Sets ext_inc dir absolute path
	 *
	 * @param string $ext_inc
	 */
	public function set_ext_inc($ext_inc = NULL) {
		if ($ext_inc != NULL) {
			$this->_set_ext_inc($ext_inc);
		} else {
			$this->_set_ext_inc($this->get_root().'/ext_inc/');
		}
	}


	/**
	 * Sets ext_inc dir absolute path
	 *
	 * @param string $ext_inc
	 * @throws TE_Web_Site_Config_Error
	 */
	private function _set_ext_inc($ext_inc) {
		if (tangra_dir_exists($ext_inc)) {
			$this->ext_inc = tangra_normalize_path($ext_inc);
		} else {
			throw new TE_Web_Site_Config_Error('Invalid $ext_inc (MS_EXT_INC). Not exists or is not a dir. Current value: '.$ext_inc);
		}
	}


	/**
	 * Returns ext_inc dir absolute path
	 *
	 * @return string
	 */
	public function get_ext_inc() {
		return $this->ext_inc;
	}
}

